package com.snut.tomorrowNews.controller.user;

import com.snut.tomorrowNews.common.CommonResult;
import com.snut.tomorrowNews.model.Collect;
import com.snut.tomorrowNews.model.Comment;
import com.snut.tomorrowNews.model.User;
import com.snut.tomorrowNews.service.user.UserService;
import com.snut.tomorrowNews.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 用户控制层
 *
 * @author Deevan
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户注册
     */
    @RequestMapping("/reg")
    public CommonResult regUser(User user) {
        CommonResult commonResult = null;
        try {
            userService.userReg(user);
            commonResult = new CommonResult<>(200, "注册成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户登录验证
     */
    @RequestMapping("/login")
    public CommonResult<User> loginUser(User user, HttpSession session) {
        CommonResult<User> commonResult = null;
        try {
            User userBack = userService.loginUser(user);
            if (userBack != null) {
                session.setAttribute("user", userBack);
                commonResult = new CommonResult<>(200, "登录成功", userBack);
            } else {
                commonResult = new CommonResult<>(201, "账号密码错误", null);
            }
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 安全退出
     */
    @RequestMapping(path = "/exit")
    public void exit(HttpSession session) {
        session.invalidate();
    }

    /**
     * 用户中心通过id获取用户信息
     */
    @RequestMapping("/getUserMsg")
    public CommonResult<User> getUserMsg(@RequestParam("userId") Integer id) {
        CommonResult<User> commonResult = null;
        try {
            User userBack = userService.getUserMsg(id);
            if (userBack != null) {
                commonResult = new CommonResult<>(200, "登录成功", userBack);
            }
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户中心文件改变时上传
     */
    @RequestMapping("/setUserFile")
    public CommonResult<String> setUserFile(@RequestParam("userFile") CommonsMultipartFile file, HttpSession session) {
        CommonResult<String> commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        File f = new File("D:\\test\\tomorrowNewsFiles\\user\\" + userId + "");
        if (!f.exists()) {
            f.mkdir();
        }
        //防止文件重名,生成保存其新文件名
        String oldFileName = file.getOriginalFilename();
        String newFileName = StringUtil.getNewFileName(oldFileName);
        File f1 = new File(f, newFileName);
        try {
            //将文件输出到指定的目录
            file.transferTo(f1);

            //返回文件名
            commonResult = new CommonResult<>(200, "上传成功,保存后更换", newFileName);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult = new CommonResult<>(500, "上传失败", null);
        }
        return commonResult;
    }

    /**
     * 用户中心修改用户信息提交保存
     */
    @RequestMapping("/setMsg")
    public CommonResult setMsg(User user) {
        CommonResult commonResult = null;
        try {
            userService.setUserMsg(user);
            commonResult = new CommonResult<>(200, "修改成功,请重新登录", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户中心修改用户密码提交
     */
    @RequestMapping("/setPassword")
    public CommonResult setPassword(@RequestParam("oldPwd") String oldPwd, @RequestParam("newPwd") String newPwd, HttpSession session) {
        CommonResult commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        try {
            Integer flag = userService.setPassword(oldPwd, newPwd, userId);
            if (flag == 1) {
                commonResult = new CommonResult<>(200, "修改成功,请重新登录", null);
            } else {
                commonResult = new CommonResult<>(201, "原密码错误", null);
            }
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户中心查询收藏列表
     */
    @RequestMapping("/getUserCollect")
    public CommonResult<List<Collect>> getUserCollect(HttpSession session) {
        CommonResult<List<Collect>> commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        try {
            List<Collect> collectList = userService.getUserCollect(userId);
            commonResult = new CommonResult<>(200, "查询成功", collectList);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户中心查询评论列表
     */
    @RequestMapping("/getUserComment")
    public CommonResult<List<Comment>> getUserComment(HttpSession session) {
        CommonResult<List<Comment>> commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        try {
            List<Comment> commentList = userService.getUserComment(userId);
            commonResult = new CommonResult<>(200, "查询成功", commentList);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }


    /**
     * 用户中心取消收藏
     */
    @RequestMapping("/deleteCollect")
    public CommonResult deleteCollect(@RequestParam("collectNewsId") Integer collectNewsId, HttpSession session) {
        CommonResult commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        try {
            userService.deleteCollect(userId, collectNewsId);
            commonResult = new CommonResult<>(200, "删除成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 用户中心删除评论
     */
    @RequestMapping("/deleteComment")
    public CommonResult deleteComment(@RequestParam("commentId") Integer commentId, HttpSession session) {
        CommonResult commonResult = null;
        try {
            userService.deleteComment(commentId);
            commonResult = new CommonResult<>(200, "删除成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 文章中点击收藏
     */
    @RequestMapping("/addCollect")
    public CommonResult addCollect(@RequestParam("collectNewsId") Integer collectNewsId, HttpSession session) {
        CommonResult commonResult = null;
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        try {
            userService.addCollect(userId, collectNewsId);
            commonResult = new CommonResult<>(200, "评论成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 查询用户列表
     */
    @RequestMapping("/getUserList")
    public CommonResult<List<User>> getUserList(@RequestParam("userType") Integer userType) {
        CommonResult<List<User>> commonResult = null;
        try {
            List<User> userList = userService.filterUserList(userType);
            commonResult = new CommonResult<>(200, "查询成功", userList);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 修改用户状态
     */
    @RequestMapping("/updateUserState")
    public CommonResult updateUserState(Integer userId,Integer state) {
        CommonResult commonResult = null;;
        try {
            userService.updateUserState(userId, state);
            commonResult = new CommonResult<>(200, "修改成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }


}
